package com.lyy.weather.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lyy.weather.bean.AirIndices;
import com.lyy.weather.bean.City;
import com.lyy.weather.bean.LifeIndices;
import com.lyy.weather.bean.WeatherDaily;
import com.lyy.weather.bean.WeatherHourly;
import com.lyy.weather.bean.WeatherTimely;
import com.lyy.weather.network.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//处理接收数据的工具类
public class DataSolver {

    private Context mContext;
    private DatabaHelper dbHelper;
    public DataSolver(Context mContext,DatabaHelper dbHelper) {
        this.mContext = mContext;
        this.dbHelper=dbHelper;
    }

    //把数据解析为Json，并传入数据库中
    public void solveResponseData(String responseData,int requestType,String locationId){

        switch (requestType){
            case HttpUtil.MyRequestType.CITY_REQUEST:
                solveCityData(responseData);
                break;
            case HttpUtil.MyRequestType.WEATHER_TIMELY_REQUEST:
                solveWeatherTimelyData(responseData,locationId);
                break;
            case HttpUtil.MyRequestType.WEATHER_HOURLY_REQUEST:
                solveWeatherHourlyData(responseData,locationId);
                break;
            case HttpUtil.MyRequestType.WEATEHR_WEEKLY_REQUEST:
                solveWeatherWeeklyData(responseData,locationId);
                    break;
            case HttpUtil.MyRequestType.LIFE_INDICES_REQUEST:
                solveLifeIndicesData(responseData,locationId);
                break;
            case HttpUtil.MyRequestType.AIR_INDICES_REQUEST:
                solveAirIndicesData(responseData,locationId);
                break;
            default:
        }
    }

    private  void solveCityData(String responseData)  {
        try{
            JSONObject jsonObject=new JSONObject(responseData);
            if(jsonObject.getInt("code")==200){
                JSONArray jsonArray=jsonObject.getJSONArray("location");
                JSONObject jb;
                Gson gson=new Gson();
                for (int i = 0; i <jsonArray.length() ; i++) {
                    jb=jsonArray.getJSONObject(i);
                    City city=gson.fromJson(jb.toString(),City.class);
                   // Log.d("TAG", city.toString());
                    //把数据整到数据库中
                    DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                    databaseUtil.addSpecificTable(HttpUtil.MyRequestType.CITY_REQUEST,city,null);

                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private  void solveLifeIndicesData(String responseData,String locationId){
        try{
            JSONObject jsonObject=new JSONObject(responseData);
            if(jsonObject.getInt("code")==200){
                //添加前把旧数据全部删除：
              //  SQLiteDatabase db=dbHelper.getWritableDatabase();
              //  db.delete("LifeIndices","locationId=?",new String[]{locationId});

                JSONArray jsonArray=jsonObject.getJSONArray("daily");
                JSONObject jb;
                Gson gson=new Gson();
                for (int i = 0; i <jsonArray.length() ; i++) {
                    jb=jsonArray.getJSONObject(i);
                    LifeIndices.DifferentIndices differentIndices=gson.fromJson(jb.toString(), LifeIndices.DifferentIndices.class);
                  //   Log.d("TAG", differentIndices.toString());
                    //把数据整到数据库中
                    DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                    databaseUtil.addSpecificTable(HttpUtil.MyRequestType.LIFE_INDICES_REQUEST,differentIndices,locationId);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private  void solveAirIndicesData(String responseData,String locationId){
        try{
            JSONObject jsonObject=new JSONObject(responseData);
            if(jsonObject.getInt("code")==200){
                 //添加前把旧数据全部删除：
              //  SQLiteDatabase db=dbHelper.getWritableDatabase();
              //  db.delete("AirIndices","locationId=?",new String[]{locationId});


                JSONObject jb=jsonObject.getJSONObject("now");
                Gson gson=new Gson();
                AirIndices airIndices=gson.fromJson(jb.toString(),AirIndices.class);
                //Log.d("TAG", jb.toString());
                DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                databaseUtil.addSpecificTable(HttpUtil.MyRequestType.AIR_INDICES_REQUEST,airIndices,locationId);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private  void solveWeatherWeeklyData(String responseData,String locationId){
        try{
            JSONObject jsonObject=new JSONObject(responseData);
            if(jsonObject.getInt("code")==200){
                //添加前把旧数据全部删除：
              //  SQLiteDatabase db=dbHelper.getWritableDatabase();
              //  db.delete("WeatherDailyData","locationId=?",new String[]{locationId});

                JSONArray jsonArray=jsonObject.getJSONArray("daily");
                JSONObject jb;
                Gson gson=new Gson();
                for (int i = 0; i <jsonArray.length() ; i++) {
                    jb=jsonArray.getJSONObject(i);
                    WeatherDaily weatherDaily =gson.fromJson(jb.toString(),WeatherDaily.class);
                  //   Log.d("TAG", jb.toString());
                    //把数据整到数据库中
                    DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                    databaseUtil.addSpecificTable(HttpUtil.MyRequestType.WEATEHR_WEEKLY_REQUEST,weatherDaily,locationId);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private  void solveWeatherHourlyData(String responseData,String locationId){
        try{
          //  Log.d("TAG","solving~");
            JSONObject jsonObject=new JSONObject(responseData);

            if(jsonObject.getInt("code")==200){
                JSONArray jsonArray=jsonObject.getJSONArray("hourly");
               // Log.d("TAG", jsonArray.toString());
                JSONObject jb;
                //添加前把旧数据全部删除：
               // SQLiteDatabase db=dbHelper.getWritableDatabase();
               // db.delete("WeatherHourlyData","locationId=?",new String[]{locationId});
                Gson gson=new Gson();
                for (int i = 0; i <jsonArray.length() ; i++) {
                  //  Log.d("TAG", "gson~");
                    jb=jsonArray.getJSONObject(i);
                  //  Log.d("TAG", jb.toString());

                    WeatherHourly weatherHourly=gson.fromJson(jb.toString(),WeatherHourly.class);


                   // Log.d("TAG", weatherHourly.toString());
                    //把数据整到数据库中
                    DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                    databaseUtil.addSpecificTable(HttpUtil.MyRequestType.WEATHER_HOURLY_REQUEST,weatherHourly,locationId);

                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private  void solveWeatherTimelyData(String responseData,String locationId){
        try{
            //添加前把旧数据全部删除：
           // SQLiteDatabase db=dbHelper.getWritableDatabase();
           // db.delete("WeatherTimelyData","locationId=?",new String[]{locationId});

            JSONObject jsonObject=new JSONObject(responseData);
            if(jsonObject.getInt("code")==200){

                JSONObject jb=jsonObject.getJSONObject("now");
                Gson gson=new Gson();
               WeatherTimely weatherTimely=gson.fromJson(jb.toString(),WeatherTimely.class);

                DatabaseUtil databaseUtil=new DatabaseUtil(mContext,dbHelper);
                databaseUtil.addSpecificTable(HttpUtil.MyRequestType.WEATHER_TIMELY_REQUEST,weatherTimely,locationId);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
